package cli.state;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

public class CurrentPath {
    private Path currentPath = Paths.get(System.getProperty("user.dir"));

    public Path get() {
        refresh();
        return currentPath;
    }

    private void refresh() {
        set(currentPath);
    }

    public boolean set(Path path) {
        try {
            currentPath = path.toRealPath();
            return true;
        } catch (IOException e) {
            try {
                currentPath = currentPath.toRealPath();
            } catch (IOException ex) {
                try {
                    currentPath = Paths.get(System.getProperty("user.dir")).toRealPath();
                } catch (IOException exc) {
                    exc.printStackTrace();
                }
            }
        }
        return false;
    }
}
